package com.dlyk.web;

import com.alibaba.excel.EasyExcel;
import com.dlyk.model.TClue;
import com.dlyk.model.TUser;
import com.dlyk.query.ClueQuery;
import com.dlyk.result.R;
import com.dlyk.service.ClueService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author: ajie
 * @date: 2024/7/24 20:46:19
 * @version: 1.0
 */
@RestController
public class ClueController {

    @Resource
    private ClueService clueService;

    /**
     * @description: 线索列表分页查询
     * @params: [current]
     * @return:
     * @author: ajie
     * @date: 2024/7/24 20:47
     */
    @PreAuthorize("hasAuthority('clue:list')")
    @GetMapping("api/clues")
    public R cluePage(@RequestParam(value = "current",required = false) Integer current,ClueQuery clueQuery){
        if (current == null){
            current = 1;
        }
        PageInfo<TClue> pageInfo = clueService.getClueByPage(current,clueQuery);
        return R.OK(pageInfo);
    }

    /**
     * @description: 导入excel文件
     * @params: [file, token]
     * @return:
     * @author: ajie
     * @date: 2024/7/27 23:05
     */
    @PreAuthorize("hasAuthority('clue:import')")
    @PostMapping("api/importExcel")
    public R importExcel(@RequestParam("file") MultipartFile file,
                         @RequestHeader(value = "Authorization") String token) throws IOException {
        clueService.importExcel(file,token);
        return R.OK();
    }

    /**
     * @description: 查询是否有该手机号码
     * @params: [phone]
     * @return:
     * @author: ajie
     * @date: 2024/7/27 23:06
     */
    @GetMapping("api/clue/{phone}")
    public R checkPhone(@PathVariable(value = "phone") String phone){
        boolean check = clueService.checkPhone(phone);
        return check ? R.OK() : R.FAIL();
    }

    /**
     * @description: 新增线索
     * @params: [clueQuery, token]
     * @return:
     * @author: ajie
     * @date: 2024/7/27 23:45
     */
    @PreAuthorize("hasAuthority('clue:add')")
    @PostMapping("api/clue")
    public R addClue(ClueQuery clueQuery,@RequestHeader(value = "Authorization") String token){

        clueQuery.setToken(token);
        int save = clueService.saveClue(clueQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 根据id查询线索详情
     * @params: [id]
     * @return:
     * @author: ajie
     * @date: 2024/7/27 23:54
     */
    @PreAuthorize("hasAuthority('clue:view')")
    @GetMapping("api/clue/detail/{id}")
    public R loadClue(@PathVariable(value = "id") Integer id){
        TClue tClue = clueService.getClueById(id);
        return R.OK(tClue);
    }

    /**
     * @description: 编辑线索
     * @params: [clueQuery, token]       
     * @return: 
     * @author: ajie
     * @date: 2024/7/31 18:17
     */
    @PreAuthorize("hasAuthority('clue:edit')")
    @PutMapping("api/clue")
    public R editClue(ClueQuery clueQuery,@RequestHeader(value = "Authorization") String token){

        clueQuery.setToken(token);
        int update = clueService.updateClue(clueQuery);
        return update >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 根据id删除线索 
     * @params: [id]       
     * @return: 
     * @author: ajie
     * @date: 2024/7/31 18:16
     */
    @PreAuthorize("hasAuthority('clue:delete')")
    @DeleteMapping("api/clue/{id}")
    public R delClue(@PathVariable(value = "id") Integer id){
        int del = clueService.delClueById(id);
        return del >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 批量删除线索
     * @params: [ids]
     * @return:
     * @author: ajie
     * @date: 2024/7/31 18:32
     */
    @PreAuthorize("hasAuthority('clue:delete')")
    @DeleteMapping("api/clue")
    public R batchDelClue(@RequestParam(value = "ids") String ids){
        //ids = "1,3,5,7";
        List<String> idList = Arrays.asList(ids.split(","));
        int batchDel = clueService.batchDelClueByIds(idList);
        return batchDel >= idList.size() ? R.OK() : R.FAIL();
    }
}
